package com.artisan.springuitls.demos;

import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * FileCopyUtils 工具类演示
 * 功能：文件拷贝终极方案
 * 优势：支持流/字节数组/文件互拷，比 Apache Commons IO 更轻量
 */
@Component
public class FileCopyUtilsDemo {

    /**
     * 场景：上传文件后保存到本地
     */
    public void saveFile(InputStream inputStream, String targetPath) throws IOException {
        System.out.println("\n========== FileCopyUtils Demo ==========");
        
        File targetFile = new File(targetPath);
        // ✅ 一行拷贝：自动关闭流
        FileCopyUtils.copy(inputStream, new FileOutputStream(targetFile));
        System.out.println("文件已保存到: " + targetFile.getAbsolutePath());
    }

    /**
     * 演示更多文件操作方法
     */
    public void demonstrateMoreMethods() throws IOException {
        System.out.println("\n--- 各种文件拷贝操作 ---");
        
        // 1. 字符串写入文件
        String content = "Hello, Spring Boot!";
        File tempFile = File.createTempFile("spring-", ".txt");
        FileCopyUtils.copy(content.getBytes(StandardCharsets.UTF_8), tempFile);
        System.out.println("✓ 字符串已写入文件: " + tempFile.getAbsolutePath());
        
        // 2. 文件读取为字节数组
        byte[] bytes = FileCopyUtils.copyToByteArray(tempFile);
        System.out.println("✓ 文件内容: " + new String(bytes, StandardCharsets.UTF_8));
        System.out.println("✓ 文件大小: " + bytes.length + " bytes");
        
        // 3. 文件复制
        File copyFile = new File(tempFile.getParent(), "copy-" + tempFile.getName());
        FileCopyUtils.copy(tempFile, copyFile);
        System.out.println("✓ 文件已复制到: " + copyFile.getAbsolutePath());
        
        // 4. 字符串读写（使用 Reader/Writer）
        String text = "Spring Framework 工具类演示";
        StringReader reader = new StringReader(text);
        StringWriter writer = new StringWriter();
        FileCopyUtils.copy(reader, writer);
        System.out.println("✓ 复制后的文本: " + writer.toString());
        
        // 5. 流转字节数组
        ByteArrayInputStream bais = new ByteArrayInputStream("测试数据".getBytes(StandardCharsets.UTF_8));
        byte[] data = FileCopyUtils.copyToByteArray(bais);
        System.out.println("✓ 流数据: " + new String(data, StandardCharsets.UTF_8));
        
        // 清理临时文件
        tempFile.delete();
        copyFile.delete();
        
        System.out.println("==========================================\n");
    }

    /**
     * 演示文件上传场景
     */
    public void simulateFileUpload() throws IOException {
        System.out.println("\n--- 模拟文件上传 ---");
        
        // 模拟上传的文件内容
        String uploadContent = "用户上传的文件内容\n第二行数据\n第三行数据";
        ByteArrayInputStream uploadStream = new ByteArrayInputStream(
            uploadContent.getBytes(StandardCharsets.UTF_8));
        
        // 保存到临时目录
        String uploadPath = System.getProperty("java.io.tmpdir") + "/uploaded-file.txt";
        FileCopyUtils.copy(uploadStream, new FileOutputStream(uploadPath));
        System.out.println("✓ 文件上传成功: " + uploadPath);
        
        // 读取验证
        File uploadedFile = new File(uploadPath);
        String savedContent = new String(FileCopyUtils.copyToByteArray(uploadedFile), StandardCharsets.UTF_8);
        System.out.println("✓ 验证文件内容:\n" + savedContent);
        
        // 清理
        uploadedFile.delete();
        
        System.out.println("==========================================\n");
    }
}

